package com.itheima.task;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author xiaycbstart
 * @create 2019 12 23 18:33
 */
@Component
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    @Scheduled(cron="0/30 * * * * ?")
    public void clearImg() {
        Set<String> fileNames = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (fileNames != null) {
            for (String fileName : fileNames) {
                QiniuUtils.deleteFileFromQiniu(fileName);
                //从集合中删除文件名
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
                System.out.println("清理了图片"+fileName);
            }
        }
    }
}
